package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.LeaderShipBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Employee;
import com.sp.scmdb.entity.LeaderShip;
import com.sp.scmdb.entity.ShiftsDetails;
import com.sp.scmdb.entity.ViewLeadership;
import com.sp.scmdb.service.LeadershipService;

@Controller("leaderShipAction")
@Scope("prototype")
public class LeaderShipAction extends BaseAction<LeaderShipBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private LeadershipService leadershipService;

	private static Log log = LogFactory.getLog(LeaderShipAction.class);

	@Autowired
	public void setLeadershipService(LeadershipService leadershipService) {
		this.leadershipService = leadershipService;
	}

	@SuppressWarnings("unchecked")
	public String doFind() {
		try {
			List<ViewLeadership> leaderShips = leadershipService
					.findAll(entity);
			this
					.button(
							"Super.Popup(5,'添加排班','/SCMDB/leaderShipAction!doNew.action','',400,250)",
							"Super.editData('/SCMDB/leaderShipAction!doFindByID.action?id=',400,250)",
							"Super.delData('/SCMDB/leaderShipAction!doDel.action?id=')");
			session.put("leaderShips", leaderShips);
		} catch (Exception e) {
			log.error("find leadership error!reason:" + e.getMessage());
			return "fail";
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String doUploadFind() {
		try {
			List<ViewLeadership> leaderShips = leadershipService
					.findAll(entity);
			session.put("leaderShips", leaderShips);
		} catch (Exception e) {
			log.error("find leadership error!reason:" + e.getMessage());
			return "fail";
		}
		return "upload";
	}

	public String doUpload() {
		String result = "";
		try {
			result = leadershipService.uplaodLeadership("");
			if (result.equals("timeOut")) {
				messageSession(CommonParamter.CHECK_FLG, "连接超时");
			} else if (result.equals("success")) {
				messageSession(CommonParamter.CHECK_SUCCESS, "上传数据");
			} else if (result.equals(CommonParamter.CHECK_FAIL)) {
				messageSession(CommonParamter.CHECK_FAIL, "上传数据");
			}else{
				messageSession(CommonParamter.CHECK_FLG,result);
			}
		} catch (Exception e) {
			log.error("find leadership error!reason:" + e.getMessage());
			return "fail";
		}
		return "upload";
	}

	@SuppressWarnings("unchecked")
	public String doNew() {
		try {
			List<Employee> empLists = leadershipService.leaderEmps();
			session.put("empLists", empLists);
			List<ShiftsDetails> shifts = leadershipService.shiftList();
			session.put("shifts", shifts);
		} catch (Exception e) {
			log.error("doNew leadership error!reason:" + e.getMessage());
			return "fail";
		}
		return "newSucc";
	}

	public String doAdd() {
		String result = CommonParamter.CHECK_FAIL;
		try {
			result = leadershipService.addLeadership(entity);
			messageSession(result, "修改排班");
		} catch (RuntimeException run) {
			log.error("add leadership faild!reason:" + run.getMessage());
			messageSession(result, "修改排班");
		} catch (Exception e) {
			log.error("add leadership faild!reason:" + e.getMessage());
			messageSession(result, "修改排班");
		}
		return "commitSucc";
	}

	@SuppressWarnings("unchecked")
	public String doFindByID() {
		try {
			String delID = request.getParameter("id");
			List<Employee> empLists = leadershipService.leaderEmps();
			session.put("empLists", empLists);
			List<ShiftsDetails> shifts = leadershipService.shiftList();
			session.put("shifts", shifts);
			LeaderShip leaderShip = leadershipService.findLeaderBean(delID);
			entity.setLeadershiftID(leaderShip.getId());
			entity.setLeaderName(leaderShip.getEmpID());
			entity.setWorkTime(leaderShip.getWorkTime().toString("yyyy-MM-dd"));
			entity.setShiftDetailName(leaderShip.getShiftsDetailID());
		} catch (Exception e) {
			log.error("doNew leadership error!reason:" + e.getMessage());
			return "fail";
		}
		return "editSucc";
	}

	public String doUpdate() {
		String result = CommonParamter.CHECK_FAIL;
		try {
			result = leadershipService.updateLeadership(entity);
			messageSession(result, "修改排班");
		} catch (RuntimeException run) {
			log.error("add leadership faild!reason:" + run.getMessage());
			messageSession(result, "修改排班");
		} catch (Exception e) {
			log.error("add leadership faild!reason:" + e.getMessage());
			messageSession(result, "修改排班");
		}
		return "commitSucc";
	}

	public String doDel() {
		String result = CommonParamter.CHECK_FAIL;
		try {
			String delID = request.getParameter("id");
			if (StringUtils.isNotEmpty(delID)) {
				result = leadershipService.delLeadership(delID);
				messageSession(result, "删除排班");
			}
		} catch (RuntimeException run) {
			log.error("del leadership faild!reason:" + run.getMessage());
			messageSession(result, "删除排班");
		} catch (Exception e) {
			log.error("del leadership faild!reason:" + e.getMessage());
			messageSession(result, "删除排班");
		}
		return "commitSucc";
	}

}
